package com.alliky.user.mvp.presenter;

import com.alliky.core.entity.vo.GoodsVO;
import com.alliky.core.base.BasePresenter;
import com.alliky.core.net.BaseObserver;
import com.alliky.core.utils.RxUtils;
import com.alliky.user.mvp.contract.MyContract;
import com.alliky.user.mvp.model.MyModel;

import java.util.HashMap;
import java.util.List;

import io.reactivex.disposables.Disposable;

public class MyPresenter extends BasePresenter<MyModel, MyContract.View> implements MyContract.Presenter {

    public MyPresenter(MyContract.View view) {
        super(view);
    }

    @Override
    protected MyModel createModel() {
        return new MyModel();
    }

    @Override
    public void accept(Disposable disposable) throws Exception {
        addSubscribe(disposable);
    }

    @Override
    public void getRecommendGoods(HashMap hashMap, boolean isRefresh) {
        if (!isViewAttached()) return;
        mModel.getRecommendGoods(hashMap)
                .compose(RxUtils.bindToLifecycle(getLifecycleProvider()))// 请求与View周期同步
                .compose(RxUtils.schedulersTransformer())// 线程调度
                .compose(RxUtils.exceptionTransformer())// 网络错误的异常转换
                .doOnSubscribe(this)
                .subscribe(new BaseObserver<List<GoodsVO>>(mRootView, false) {
                    @Override
                    public void onResult(List<GoodsVO> list) {
                        mRootView.showRecommendGoods(list, isRefresh);
                        if (isRefresh) {
                            mRootView.finishRefresh();
                        } else {
                            mRootView.finishLoadMore();
                        }
                        mRootView.setPageNum((int) hashMap.get("pageNum") + 1);
                    }
                });
    }
}
